/*
 * Copyright (c) 2007 Compl Yue Still, All Rights Reserved.
 */

package hbi.wire.as3.host
{
	import flash.utils.*;
	import hbi.task.as3.AgentMethodInvocation;
	
	public class MethodInvocation implements IncomingTag
	{
		
		private var _script:TaskScript;
		
		public function MethodInvocation(script:TaskScript)
		{
			this._script = script;
		}
		
		private var method:String = null;
		
		private var argTags:DataTagList = null;
		
		public function processIncomingData(input:IDataInput):Boolean
		{
			if(input.bytesAvailable < 1) return false;
			
			if(method == null)
			{
				if(input.bytesAvailable < 2) return false;
				var methodIndex:uint = input.readUnsignedShort();
				method = _script.agentType.getResolvedMethod(methodIndex);
				argTags = new DataTagList()
				if(input.bytesAvailable < 1) return false;
			}
			
			// assert argTags != null;
			if(argTags.processIncomingData(input))
			{
				_script.invocations.push(new AgentMethodInvocation
					(method, argTags.tagValues));
				argTags = null;
				return true;
			}
			
			return false;
		}
		
	}
}
